package com.zhang.gmall.realtime.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @title:
 * @author: zhang
 * @date: 2022/3/8 11:48
 */
public class JedisUtil {
    private static JedisPool jedisPool = null;

    public static Jedis getJedis() {
        if (jedisPool == null) {
            synchronized (JedisUtil.class) {
                if (jedisPool == null) {
                    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
                    jedisPoolConfig.setMaxTotal(1000); //设置最大连接数
                    jedisPoolConfig.setBlockWhenExhausted(true);//连接耗尽是否等待
                    jedisPoolConfig.setMaxWaitMillis(2000);//等待时间
                    jedisPoolConfig.setMaxIdle(5);//设置最大闲置连接数
                    jedisPoolConfig.setMinIdle(5);//设置最小闲置连接数
                    jedisPoolConfig.setTestOnBorrow(true);//取连接的时候进行一下测试ping pong
                    jedisPool = new JedisPool(jedisPoolConfig, "hadoop103", 6379);
                    System.out.println("开辟连接池");

                }
            }
        }
        return jedisPool.getResource();
    }


    public static void main(String[] args) {
        Jedis jedis = getJedis();
        System.out.println(jedis.ping());
    }
}
